package cn.hbu.zlfw.patent.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.hbu.zlfw.annotation.OperatorLog;
import cn.hbu.zlfw.annotation.OperatorType;
import cn.hbu.zlfw.common.util.SessionUtil;
import cn.hbu.zlfw.dic.entity.DicPatentState;
import cn.hbu.zlfw.dic.service.DicSubjectService;
import cn.hbu.zlfw.patent.entity.Patent;
import cn.hbu.zlfw.patent.entity.PatentSynergy;
import cn.hbu.zlfw.patent.service.PatentService;
import cn.hbu.zlfw.patent.service.PatentSynergyService;
import cn.hbu.zlfw.research.entity.Researcher;

/**
 * 
 * @ClassName: PatentSynergyController 
 * @Description: 专利协同申请书控制层
 * @author 王鹏伟<1223443289@qq.com>
 * @date 2017年11月3日 上午10:59:29  
 * @version V1.0
 */
@Controller("patentSynergyController")
@RequestMapping("/patentSynergy")
public class PatentSynergyController {
	/**
	 * 日志
	 */
	public final Logger logger=LoggerFactory.getLogger(PatentSynergyController.class);
	/**
	 * 注入HttpServletRequest
	 */
	@Autowired
	private HttpServletRequest request;
	/**
	 *注入service
	 */
	@Autowired
	private PatentSynergyService patentSynergyService;
	
	@Autowired
	private DicSubjectService dicSubjectService;
	
	@Autowired
	private PatentService patentService;
	/**
	 * @Title: toPatentSynergy 
	 * @Description: 转向专利协作申请提交页面
	 * @return String   
	 * @throws
	 */
	@OperatorLog(type=OperatorType.OTHER,description="填写专利协作申请书")
	@RequestMapping("/patentSynergy.action")
	public String toPatentSynergy(ModelMap map,@RequestParam(value="patentId",required=false)String patentId) {
		if(patentId != null && patentId != "") {	//在修改信息时进行数据回显
			map.put("patentSynergy", patentSynergyService.getNewestSynergyByPatentId(patentId));
			map.put("patent", patentService.getById(patentId));
		}
		map.put("subjects", this.dicSubjectService.findAll());
		return "zlxt/patentSynergy";
	}
	
	/**
	 * @Title: commitSynergyForm 
	 * @Description: 提交专利写作申请书
	 * @return String   
	 * @throws
	 */
	@ResponseBody
	@OperatorLog(type=OperatorType.SAVE,description="成功保存专利协作申请书")
	@RequestMapping("/commitSynergy.action")
	public String commitSynergyForm(@ModelAttribute("patentSynergy") PatentSynergy patentSynergy,
					@ModelAttribute("patent") Patent patent ) {
		//处理所属学科字段信息
		String[] subjects=request.getParameterValues("subject");
		StringBuffer sbfsubject=new StringBuffer();
		for (int i = 0; i < subjects.length; i++) {
			if(i==(subjects.length-1)) {
				sbfsubject.append(subjects[i]);
			}else {
				sbfsubject.append(subjects[i]+",");
			}
		}
		//完善专利信息
		patent.setPatentState(DicPatentState.WAIT_RECEIVE); //专利初始状态待结单状态
		patent.setSubjectTypes(sbfsubject.toString());
		patent.setDelState(0);
		patent.setCreateTime(new Date());
		patent.setUpdateTime(new Date());
		patent.setSfSynergy(1);   //已提交专利协同申请书
		patent.setResearcher((Researcher)SessionUtil.getLoginUser(request));  //由patent维护patent-researcher关系
		this.patentService.save(patent);
		//保存专利协同申请信息
		patentSynergy.setPatent(patent);	//由patentSynergy维护patentSynergy-patent关系
		patentSynergy.setDelState(0);
		patentSynergy.setUpdateTime(new Date());
		patentSynergy.setCreateTime(new Date());
		this.patentSynergyService.save(patentSynergy);
		return "success";
	}
	
	/**
	 * @Title: updateSynergyForm 
	 * @Description: 修改专利协作申请书
	 * @return String   
	 * @throws
	 */
	@ResponseBody
	@OperatorLog(type=OperatorType.SAVE_OR_UPDATE,description="修改专利协作申请书")
	@RequestMapping("/updateSynergy.action")
	public String updateSynergyForm(@ModelAttribute("patentSynergy") PatentSynergy patentSynergy,
					@RequestParam(value="patentId",required=false)String patentId,
					@RequestParam(value="allowCorss",required=false)Integer allowCorss,
					@RequestParam(value="name",required=false)String name) {
		Patent patent = this.patentService.getById(patentId);
		//处理所属学科字段信息
		String[] subjects=request.getParameterValues("subject");
		StringBuffer sbfsubject=new StringBuffer();
		for (int i = 0; i < subjects.length; i++) {
			if(i==(subjects.length-1)) {
				sbfsubject.append(subjects[i]);
			}else {
				sbfsubject.append(subjects[i]+",");
			}
		}
		//修改专利信息
		patent.setSubjectTypes(sbfsubject.toString());
		patent.setName(name);
		patent.setAllowCorss(allowCorss);
		this.patentService.update(patent);
		//保存专利协同申请信息
		patentSynergy.setPatent(patent);	//由patentSynergy维护patentSynergy-patent关系
		patentSynergy.setDelState(0);
		patentSynergy.setUpdateTime(new Date());
		patentSynergy.setCreateTime(new Date());
		this.patentSynergyService.save(patentSynergy);
		return "success";
	}
}
